package com.why.zing.payapi.exception;

import com.why.zing.common.constant.ResultCode;
import com.why.zing.common.domain.ZingResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(PayException.class)
    public ZingResult payException(HttpServletRequest request, PayException e) {
        log.error("请求的url为{}出现异常,异常信息为:{}", request.getRequestURI(), ExceptionUtils.getStackTrace(e));
        return ZingResult.error(e);
    }


    @ExceptionHandler(AliPayException.class)
    public ZingResult aliPayException(HttpServletRequest request, AliPayException e) {
        log.error("请求的url为{}出现异常,异常信息为:{}", request.getRequestURI(), ExceptionUtils.getStackTrace(e));
        return ZingResult.error(e);
    }

    @ExceptionHandler(Exception.class)
    public ZingResult exception(HttpServletRequest request, Exception e) {
        log.error("请求的url为{}出现异常,异常信息为:{}", request.getRequestURI(), ExceptionUtils.getStackTrace(e));
        return ZingResult.error(ResultCode.ERROR);
    }

}
